home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2002 November / SGI IRIX 6.5 Applications 2002 November.iso / dist / gateway.idb / usr / WebFace / Source / 20-NetworkServices / routing / gateways-config.frm.z / gateways-config.frm
Encoding:
Text File  |  2002-06-12  |  11.6 KB  |  421 lines

  1. #!/usr/bin/perl5
  2. #
  3. # gateways-config.cgi
  4. #
  5. # Copyright 1988-1996 Silicon Graphics, Inc.
  6. # All rights reserved.
  7. #
  8. # This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  9. # the contents of this file may not be disclosed to third parties, copied or
  10. # duplicated in any form, in whole or in part, without the prior written
  11. # permission of Silicon Graphics, Inc.
  12. #
  13. # RESTRICTED RIGHTS LEGEND:
  14. # Use, duplication or disclosure by the Government is subject to restrictions
  15. # as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  16. # and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  17. # successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  18. # rights reserved under the Copyright Laws of the United States.
  19. #
  20. # $Id: gateways-config.frm,v 1.37 1997/11/17 19:09:55 shotes Exp $
  21.  
  22. require "/usr/OnRamp/lib/OnRamp.pm";
  23. require "/usr/OnRamp/lib/java.pm";
  24.  
  25. $conf = "/etc/gateways";
  26. $dummy = "/etc/gateways.dummy";
  27. $myname = "gateways-config.cgi";
  28. $help_page = "gateways-config-help.html";
  29. $title = "Gateway Configuration";
  30.  
  31. $js_main =
  32. "type = \"address\";
  33. $js_account_main
  34. $js_error_box
  35. $js_help
  36. $js_ip
  37. function testAdd(form) {
  38.     if(form.destination.value == \"\") {
  39.         errorBox (form.destination, \"To add a new gateway, first enter \\na \"
  40.             + \"destination IP address.\");
  41.         return (false);
  42.     }
  43.     if (!testIPaddress(form.destination.value, false)) {
  44.         errorBox (form.destination, \"The destination address \" + 
  45.             form.destination.value + \"\\nis invalid.\");
  46.         return (false);
  47.     }
  48.     return (true);
  49. }";
  50.  
  51. $js_add =
  52. "$js_standard
  53. $js_error_box
  54. $js_help
  55. $js_ip
  56. function checkForm(form)  {
  57.     if (form.route.value == \"\") { 
  58.         errorBox(form.route, \"Route interface required.\");
  59.     }
  60.     if (!testIPaddress(form.route.value, false)) {
  61.         errorBox(form.route, \"Invalid route interface address.\");
  62.         return (false);
  63.     }
  64.     if (!testInt(form.metric)) return (false);
  65.     return (true);
  66. }
  67. function testInt(Ctrl) {
  68.     if (Ctrl.value == \"\") { 
  69.         errorBox (Ctrl, \"The number of hops is required.\"); 
  70.         return (false); 
  71.     }
  72.     start = 0;  
  73.     while (Ctrl.value.charAt(start) == ' ') { start++; }
  74.     end = Ctrl.value.length-1; 
  75.     while (Ctrl.value.charAt(end) == ' ') { end--; }
  76.     for (n = start; n <= end; n++) {
  77.         digit = Ctrl.value.charAt(n); 
  78.         if(! checkInt_c(digit, 10)) { 
  79.             errorBox (Ctrl, \"The number of hops must be \\na valid integer.\"); 
  80.             return (false); 
  81.         }
  82.     }
  83.     str = Ctrl.value.substring(start,end+1); num = parseInt(str);
  84.     if (num > 16) { 
  85.         errorBox (Ctrl, \"The number of hops must be \\nless than 16.\"); 
  86.         return (false); 
  87.     }
  88.     return (true);
  89. }";
  90.  
  91. $js_edit =
  92. "$js_standard
  93. $js_error_box
  94. $js_help
  95. $js_ip
  96. function checkForm(form) {
  97.     if (form.route.value == \"\") { 
  98.         errorBox(form.route, \"Route interface required.\");
  99.     }
  100.     if (!testIPaddress(form.route.value, false)) {
  101.         errorBox(form.route, \"Invalid route interface address.\");
  102.         return (false);
  103.     }
  104.     if (!testInt(form.metric)) return (false);
  105.     return (true);
  106. }
  107. function testInt(Ctrl) {
  108.     if (Ctrl.value == \"\") { 
  109.         errorBox (Ctrl, \"The number of hops is required.\"); 
  110.         return (false); 
  111.     }
  112.     start = 0;  
  113.     while (Ctrl.value.charAt(start) == ' ') { start++; }
  114.     end = Ctrl.value.length-1; 
  115.     while (Ctrl.value.charAt(end) == ' ') { end--; }
  116.     for (n = start; n <= end; n++) {
  117.         digit = Ctrl.value.charAt(n); 
  118.         if(! checkInt_c(digit, 10)) { 
  119.             errorBox (Ctrl, \"The number of hops must be \\na valid integer.\"); 
  120.             return (false); 
  121.         }
  122.     }
  123.     str = Ctrl.value.substring(start,end+1); num = parseInt(str);
  124.     if (num > 16) { 
  125.         errorBox (Ctrl, \"The number of hops must be \\nless than 16.\"); 
  126.         return (false); 
  127.     }
  128.     return (true);
  129. }";
  130.  
  131.  
  132. print "Content-type: text/html\n\n";
  133.  
  134. &get_fields;
  135. &getGateways;
  136.  
  137. $delete = 0;
  138.  
  139. if (%fld) {
  140.     $help = $document_root . $ENV{"SCRIPT_NAME"};
  141.     $help =~ s/cgi$/hlp/;
  142.     exec $help if ($fld{'help'} eq "Help");
  143.  
  144.     $fld{'chosen'} =~ /([\w.]+)/;
  145.     $fld{'chosen'} = $1;
  146.     if ($fld{"add"}) { 
  147.         &validAdd;
  148.         $val{'metric'} = 1;
  149.         &getAdd;
  150.         exit 0; 
  151.     }
  152.     elsif ($fld{"doAdd"}) {
  153.         &valid(0);
  154.         &doAdd;
  155.         $fld{'destination'} = "";
  156.         &getGateways;
  157.     &routed_hup;
  158.     }
  159.     elsif ($fld{"doEdit"}) {
  160.         &valid(1);
  161.         &doEdit;
  162.         &getGateways;
  163.     &routed_hup;
  164.     }
  165.     elsif ($fld{"edit"}) {
  166.         &error(2,"To edit an existing gateway, first choose one from the list,
  167.             then click the edit button.") if !$fld{'chosen'};
  168.         &getGateways($fld{'chosen'});
  169.         &putEdit;
  170.         &getEdit;
  171.         exit 0; 
  172.     }
  173.     elsif ($fld{"delete"}) { 
  174.         &error(2,"To delete an existing gateway, first choose one from the 
  175.             list, then click the delete button.") if !$fld{'chosen'};
  176.         $delete = 1;
  177.         $message = qq|Click "Ok" to save changes.|; 
  178.     }
  179.     elsif ($fld{"doit"}) { 
  180.         if ($erd ne $fld{'erd'}) { &changeStatus; }
  181.         if ($fld{'delGateway'}) { &doDelete; };
  182.         &getGateways;
  183.     &routed_hup;
  184.     }
  185.     else {
  186.         $message = "Use buttons to submit form.";
  187.     }
  188.  
  189.     if (!$message) { $message = "No change made."; }
  190.     %val = %fld; 
  191.  
  192. &generic;
  193.  
  194. sub routed_hup {
  195.     system("/sbin/killall", "routed");
  196.     
  197.     open(IN,"< /etc/config/routed.options");
  198.     while(<IN>) {
  199.         chop($_);
  200.         $configFile .= " $_";
  201.     }
  202.     close(IN);
  203.  
  204.     system("/usr/etc/routed $configFile > /dev/null 2>&1");
  205. }
  206.  
  207. sub valid {
  208.     &error($_[0],"Invalid route address.") if !$fld{'route'} || &check_ipaddr($fld{'route'});
  209.     &error($_[0],"Number of hops must be an integer.") if $fld{'metric'} =~ /[^0-9-]/;
  210.     &error($_[0],"Number of hops must be greater than zero.") if $fld{'metric'} < 1;
  211.     &error($_[0],"Number of hops must be no greater than 16.") if $fld{'metric'} > 16;    
  212.  
  213. sub validAdd {
  214.     &error(2,"You must provide a gateway address.") if !$fld{'destination'};
  215.     &error(2,"Invalid gateway address.") if &check_ipaddr($fld{'destination'});   
  216. }
  217.  
  218. sub error {
  219.     &error_block($_[1]);
  220.     %val = %fld;
  221.     if ($_[0] == 0) { &getAdd; }
  222.     elsif ($_[0] == 1) { &getEdit; }
  223.     else { &generic; }
  224.     exit 0;
  225. }
  226.  
  227. sub changeStatus {
  228.     if ($fld{'erd'} eq 'Yes') { 
  229.     system("/etc/chkconfig", "routed", "on");
  230.     system("/etc/killall", "routed");
  231.     open(IN,"< /etc/config/routed.options");
  232.     while(<IN>) {
  233.         chop($_);
  234.         $configFile .= " $_";
  235.     }
  236.     close(IN);
  237.     system("/usr/etc/routed $configFile > /dev/null 2>&1");
  238.     $message = "Dynamic routing enabled.";
  239.     }
  240.     else {
  241.     system("/etc/chkconfig", "routed", "off");
  242.     system("/etc/killall", "routed");
  243.         $message = "Dynamic routing disabled.";
  244.     }
  245. }
  246.    
  247. sub doDelete {
  248.     open(IN,"< $conf");
  249.     open(OUT,"> $dummy");
  250.     while(<IN>) {
  251.     @items = split(/\s+/);
  252.     if ($items[1] eq $fld{'delGateway'}) { print OUT "# $_"; }
  253.     else { print OUT $_; }
  254.     }
  255.     close(IN);
  256.     close(OUT);
  257.     rename($dummy,$conf);
  258.  
  259.     $message = "Gateway deleted.";
  260. }
  261.  
  262. sub doAdd {
  263.     open(OUT,">> $conf");
  264.     print OUT "$fld{'type'} $fld{'destination'} gateway $fld{'route'} ";
  265.     print OUT "metric $fld{'metric'} $fld{'mode'}\n";
  266.     close(OUT);
  267.  
  268.     $message = "Gateway added.";
  269. }
  270.  
  271. sub putEdit {
  272.     $val{'type'} = $type;
  273.     $val{'mode'} = $mode;
  274.     $val{'route'} = $rout;
  275.     $val{'metric'} = $metr;
  276. }
  277.  
  278. sub doEdit {
  279.     open(IN,"< $conf");
  280.     open(OUT,"> $dummy");
  281.     while(<IN>) {
  282.     @items = split(/\s+/);
  283.     if ($items[1] eq $fld{'chosen'}) { print OUT "# $_"; }
  284.     else { print OUT $_; }
  285.     }
  286.     print OUT "$fld{'type'} $fld{'chosen'} gateway $fld{'route'} ";
  287.     print OUT "metric $fld{'metric'} $fld{'mode'}\n";
  288.     close(IN);
  289.     close(OUT);
  290.     rename($dummy,$conf);
  291.  
  292.     $message = "Gateway edited.";
  293. }
  294.     
  295. sub getEdit {
  296.     &js_title_block($title,$js_edit);
  297.     &header_block("Edit Gateway");
  298.  
  299.     print "<form name=StandardForm action=$myname method=post onSubmit=\"return runSubmit()\">\n";
  300.  
  301.     print "<input type=hidden name=chosen value=$fld{'chosen'}>";
  302.  
  303.     print "<center><table width=450>";
  304.  
  305.     print "<tr><th align=left>IP address of gateway destination:</th><td>
  306.         <tt>$fld{'chosen'}</tt></td></tr>";
  307.     print "<tr><th align=left>IP address of route interface:</th><td>",
  308.         &text('route',$val{'route'},16),"</td></tr>";
  309.     print "<tr><th align=left>Type of network gateway:</th><td>",
  310.         &select('type',$val{'type'},'net','host'),"</td></tr>";
  311.     print "<tr><th align=left>Mode of network gateway:</th><td>",
  312.         &select('mode',$val{'mode'},'active','passive'),"</td></tr>";
  313.     print "<tr><th align=left>Number of hops from route interface to 
  314.             destination:</th><td>",
  315.         &text('metric',$val{'metric'},5),"</td></tr>";
  316.  
  317.     print "</table></center>";
  318.  
  319.     print &js_buttons('doEdit','Ok','onClick="markOK()"',
  320.         'onClick="markOther()"',
  321.         "onClick=\"do_help('$help_page'); return (false)\"");
  322.     
  323.     print '</form>';
  324. }
  325.  
  326. sub getAdd {
  327.     &js_title_block($title,$js_add);
  328.     &header_block("Add New Gateway");
  329.  
  330.     print "<form name=StandardForm action=$myname method=post onSubmit=\"return runSubmit()\">\n";
  331.  
  332.     print "<input type=hidden name=destination value=$fld{'destination'}>";
  333.  
  334.     print "<center><table width=450>";
  335.  
  336.     print "<tr><th align=left>IP address of gateway destination:</th><td>
  337.         <tt>$fld{'destination'}</tt></td></tr>";
  338.     print "<tr><th align=left>IP address of route interface:</th><td>",
  339.         &text('route',$val{'route'},16),"</td></tr>";
  340.     print "<tr><th align=left>Type of network gateway:</th><td>",
  341.         &select('type',$val{'type'},'net','host'),"</td></tr>";
  342.     print "<tr><th align=left>Mode of network gateway:</th><td>",
  343.         &select('mode',$val{'mode'},'active','passive'),"</td></tr>";
  344.     print "<tr><th align=left>Number of hops from route interface to 
  345.             destination:</th><td>",
  346.         &text('metric',$val{'metric'},5),"</td></tr>";
  347.  
  348.     print "</table></center>";
  349.  
  350.     print &js_buttons('doAdd','Ok','onClick="markOK()"','onClick="markOther()"',
  351.         "onClick=\"do_help('$help_page'); return (false)\"");
  352.     
  353.     print '</form>';
  354. }
  355.    
  356. sub generic {
  357.     &js_title_block($title,$js_main);
  358.     &header_block($title);
  359.  
  360.     print "<i>$message</i>";
  361.  
  362.     print "<form name=AccountForm action=$myname method=post onSubmit=\"return runSubmit()\">\n";
  363.  
  364.     print "<center><table width=450>\n";
  365.  
  366.     print "<tr><th align=left>Enable dynamic routing:</th><th align=left>",
  367.         &radio('erd',$erd,'Yes','No'),"</th></tr>\n";
  368.  
  369.     print "<tr><th align=left>";
  370.     print qq|<input type=submit name=add value="Add New Gateway" onClick="markAdd()"></th>|;
  371.     print "\n<td><input size=19 type=text name=destination 
  372.         value=$val{'destination'}>";
  373.     print "</td></tr>\n";
  374.  
  375.     if ($delete) { 
  376.     undef @locList;
  377.     foreach $arg (@gateways) {
  378.         if ($arg ne $fld{'chosen'}) { 
  379.         push(@locList,$arg); }
  380.     }
  381.     print "<input type=hidden name=delGateway value=$fld{'chosen'}>\n"; 
  382.     } else { @locList = @gateways; }
  383.  
  384.     print "<tr><th align=left>";
  385.     print qq|<input type=submit name=edit value="Edit Selected Gateway" onClick="markEdit()"></th>|;
  386.     print "\n<td rowspan=2>", &choice_list(*locList,"chosen",20), "</td></tr>\n";
  387.  
  388.     print "<tr><th align=left>";
  389.     print qq|<input type=submit name=delete value="Delete Selected Gateway" onClick="markDelete()"></th></tr>|;
  390.  
  391.     print "\n</table></center>\n";
  392.  
  393.     print &js_buttons('doit','Ok','onClick="markOther()"',
  394.         'onClick="markOther()"',
  395.         "onClick=\"do_help('$help_page'); return (false)\"");
  396.     
  397.     print '</form>';
  398. }
  399.  
  400. sub getGateways {
  401.     undef @gateways;
  402.  
  403.     open(GATE, "< $conf");
  404.     while (<GATE>) {
  405.     @items = split(/\s+/);
  406.     if ($items[0] ne 'net' && $items[0] ne 'host') { next; }
  407.     push(@gateways,$items[1]);
  408.     if ($items[1] eq $_[0]) {
  409.         $type = $items[0];
  410.         $rout = $items[3];
  411.         $metr = $items[5];
  412.         $mode = $items[6];
  413.     }
  414.     }
  415.     close(GATE);
  416.  
  417.     $erd = &get_config("routed");
  418. }
  419.